// pages/shopping/shopping.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: {},
    shopping_items: {},
    isLogin: 0,
    total_amount: 0,
    sub_total: [],
    btn_active: [],
    num: [],
    paying_wechat: 1,
    paying_alipay: 0
  },
  add(e) {
    var index = e.currentTarget.dataset.index;
    var num = this.data.num;
    var btn_active = this.data.btn_active;
    num[index] = num[index] + 1;
    btn_active[index] = 1;
    this.setData({
      num: num,
      btn_active: btn_active
    });
    var total = 0;
    var sub_total = [];
    this.data.sub_total.forEach((item) => {
      sub_total.push(item);
    });
    num.forEach((item, index) => {
      sub_total[index] = sub_total[index] * item;
      total += sub_total[index];
    });
    total = total.toFixed(2);
    this.setData({
      total_amount: total
    });
  },
  minus(e) {
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var sub_total = this.data.sub_total;
    if (this.data.btn_active[index] == 1) {
      var num = this.data.num;
      num[index] = num[index] - 1;
      this.setData({
        num: num
      });
      if (num[index] == 1) {
        var btn_active = this.data.btn_active;
        btn_active[index] = 0;
        this.setData({
          btn_active: btn_active
        });
      }
      var sub_total = [];
      var total = 0;
      this.data.sub_total.forEach((item) => {
        sub_total.push(item);
      });
      num.forEach((item, index) => {
        sub_total[index] = sub_total[index] * item;
        total += sub_total[index];
      });
      total = total.toFixed(2);
      this.setData({
        total_amount: total
      });
    }

  },
  changePaying(e) {
    if (e.currentTarget.dataset.index == "1") {
      if (this.data.paying_wechat == 0) {
        this.setData({
          paying_alipay: 0,
          paying_wechat: 1,
        });
        console.log(12);
      }else {
        this.setData({
          paying_wechat: 0,
          paying_alipay: 1
        });
      }
    } else if (e.currentTarget.dataset.index == "2") {
      if (this.data.paying_alipay == 0) {
        this.setData({
          paying_wechat: 0,
          paying_alipay: 1
        });
      }else {
        this.setData({
          paying_wechat: 1,
          paying_alipay: 0
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const db = wx.cloud.database();
    var that = this;
    db.collection('users').doc('44e883245f6b00b9015166a06534bafc').get({
      success: function (res) {
        if (res.data.index.length != 0) {
          var index = res.data.index;
          db.collection('users').where({
              username: index
            })
            .get({
              success: function (res) {
                // res.data 是包含以上定义的两条记录的数组
                that.setData({
                  user_info: res.data[0]
                });
              }
            });
          db.collection('shopping').where({
              username: index
            })
            .get({
              success: function (res) {
                // res.data 是包含以上定义的两条记录的数组
                that.setData({
                  shopping_items: res.data[0],
                  isLogin: 1
                });
                var sub_total = [];
                var total = 0;
                var num = [];
                var btn_active = [];
                that.data.shopping_items.shopping.forEach((item) => {
                  sub_total.push(item.price);
                  total += item.price;
                  num.push(1);
                  btn_active.push(0);
                });
                total = total.toFixed(2);
                that.setData({
                  sub_total: sub_total,
                  total_amount: total,
                  num: num
                });
              }
            });
        } else {
          that.setData({
            isLogin: 0
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const pages = getCurrentPages();
    pages[0].onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})